package com.fenghongzhang.kuaishou;

import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.fenghongzhang.kuaishou.common.Constant;
import com.fenghongzhang.kuaishou.common.RouterConstant;
import com.fenghongzhang.kuaishou.index.home.IndexFragment;
import com.fenghongzhang.kuaishou.index.local.LocalFragment;
import com.fenghongzhang.kuaishou.index.message.MessageFragment;
import com.fenghongzhang.kuaishou.index.mine.MineFragment;
import com.fenghongzhang.kuaishou.utils.ActivityStatusBarUtil;
import com.fenghongzhang.kuaishou.utils.SPUtil;
import com.fenghongzhang.kuaishou.widget.MyBottomBarItem;
import com.fenghongzhang.kuaishou.widget.MyBottomNavigationBar;
import com.google.android.material.navigation.NavigationView;
import com.umeng.message.PushAgent;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * 此项目作为练习用.不得作为商业用途.
 */
public class MainActivity extends AppCompatActivity implements MyBottomNavigationBar.OnItemSelectedListener {
    private FrameLayout mainFragmentContain;
    private BottomNavigationBar bottomNavigationBar;
    private MyBottomNavigationBar mybottomNavigationBar;
    private Map<Integer, Fragment> cacheFragment;
    private int currentFragmentId = 0 ;

    private DrawerLayout drawerLayout;
    private NavigationView navigationView;
    private RelativeLayout fMainMenuUserLayout;
    private ImageView fMainMenuAvatarIv;
    private TextView fMainMenuNameTv;
    private TextView fMainMenuTalkTv;
    private TextView fMenuGossipItem;
    private TextView fMenuMessageItem;
    private TextView fMenuChatItem;
    private TextView fMenuLargeItem;
    private TextView fMenuChildItem;
    private LinearLayout fMainMenuSettingLayout;
    private TextView fMenuSettingItem;
    private RelativeLayout mainContainer;
    private FrameLayout homeFragmentContainer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        PushAgent.getInstance(this).onAppStart();

        //修改状态栏
        ActivityStatusBarUtil.setAndroidNativeLightStatusBar(this,true);
        initView();
        initData();
        mainFragmentContain = (FrameLayout) findViewById(R.id.home_fragment_container);
        mybottomNavigationBar = (MyBottomNavigationBar) findViewById(R.id.mybottomNavigationBar);
        mybottomNavigationBar.setOnItemSelectedListener(this);
        cacheFragment = new HashMap<>();

        mybottomNavigationBar
                .addItem(new MyBottomBarItem(this).setTitle("首页"))
                .addItem(new MyBottomBarItem(this).setTitle("同城"))
                .addItem(new MyBottomBarItem(this).setDrawable(getResources().getDrawable(R.mipmap.detail_share_icon_story_normal1)))
                .addItem(new MyBottomBarItem(this).setTitle("消息"))
                .addItem(new MyBottomBarItem(this).setTitle("我"))
                .build();
        mybottomNavigationBar.setPosition(0);


    }

    private void initData() {
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
    }

    private void initView() {
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        navigationView = (NavigationView) findViewById(R.id.navigation_view);
        fMainMenuUserLayout = (RelativeLayout) findViewById(R.id.f_main_menu_user_layout);
        fMainMenuAvatarIv = (ImageView) findViewById(R.id.f_main_menu_avatar_iv);
        fMainMenuNameTv = (TextView) findViewById(R.id.f_main_menu_name_tv);
        fMainMenuTalkTv = (TextView) findViewById(R.id.f_main_menu_talk_tv);
        fMenuGossipItem = (TextView) findViewById(R.id.f_menu_gossip_item);
        fMenuMessageItem = (TextView) findViewById(R.id.f_menu_message_item);
        fMenuChatItem = (TextView) findViewById(R.id.f_menu_chat_item);
        fMenuLargeItem = (TextView) findViewById(R.id.f_menu_large_item);
        fMenuChildItem = (TextView) findViewById(R.id.f_menu_child_item);
        fMainMenuSettingLayout = (LinearLayout) findViewById(R.id.f_main_menu_setting_layout);
        fMenuSettingItem = (TextView) findViewById(R.id.f_menu_setting_item);
        mainContainer = (RelativeLayout) findViewById(R.id.main_container);
        homeFragmentContainer = (FrameLayout) findViewById(R.id.home_fragment_container);
    }


    @Override
    protected void onRestart() {
        super.onRestart();
        String userName = SPUtil.getInstance().getSP().getString("userName", "nobody");
        IndexFragment fragmentByTag = (IndexFragment) getSupportFragmentManager().findFragmentByTag(String.valueOf(R.id.index_fragment));
        //有人登录
        if (!userName.equals("nobody")){  //登录了
            fragmentByTag.getView().findViewById(R.id.login).setVisibility(View.GONE);
            fragmentByTag.getView().findViewById(R.id.slideMenu).setVisibility(View.VISIBLE);
            drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
        }else{ //没有登录
            fragmentByTag.getView().findViewById(R.id.login).setVisibility(View.VISIBLE);
            fragmentByTag.getView().findViewById(R.id.slideMenu).setVisibility(View.GONE);
        }
    }


    public void showFragment(int fragmentId){
        if(currentFragmentId == 0){
            getSupportFragmentManager().beginTransaction().add(R.id.home_fragment_container,getFragment(fragmentId),String.valueOf(fragmentId)).commit();
        }else {
            if(getFragment(fragmentId).isAdded()){
                getSupportFragmentManager().beginTransaction().hide(getFragment(currentFragmentId)).show(getFragment(fragmentId)).commit();
            }else{
                getSupportFragmentManager().beginTransaction().hide(getFragment(currentFragmentId)).add(R.id.home_fragment_container,getFragment(fragmentId),String.valueOf(fragmentId)).commit();
            }
        }
        currentFragmentId = fragmentId;
    }

    //根据id来存缓存中查找fragment
    public Fragment getFragment(int fragmentId){
        switch (fragmentId){
            case R.id.index_fragment:
                if(!cacheFragment.containsKey(fragmentId)){
                    cacheFragment.put(fragmentId,new IndexFragment());
                }
                break;
            case R.id.local_fragment:
                if(!cacheFragment.containsKey(fragmentId)){
                    cacheFragment.put(fragmentId,new LocalFragment());
                }
                break;
            case R.id.message_fragment:
                if(!cacheFragment.containsKey(fragmentId)){
                    cacheFragment.put(fragmentId,new MessageFragment());
                }
                break;
            case R.id.mine_fragment:
                if(!cacheFragment.containsKey(fragmentId)){
                    cacheFragment.put(fragmentId,new MineFragment());
                }
                break;
        }
        return cacheFragment.get(fragmentId);
    }


    @Override
    public void onTabSelected(int position) {
        //切换bar的颜色
        mybottomNavigationBar.selectTextStyle(position);
        switch (position){

            case Constant.INDEX_FRAGMENT:
                showFragment(R.id.index_fragment);
                break;
            case Constant.LOCAL_FRAGMENT:
                showFragment(R.id.local_fragment);
                break;
            case Constant.OPEN_CAMERA:
                ARouter.getInstance().build(RouterConstant.CAMERA_MAIN_ACTIVITY).navigation();
                break;
            case Constant.MESSAGE_FRAGMENT:
                showFragment(R.id.message_fragment);
                break;
            case Constant.MINE_FRAGMENT:
                showFragment(R.id.mine_fragment);
                break;
        }

    }

    @Override
    public void onTabUnselected(int position) {
    }


    public static void launch(Activity activity){
        Intent intent = new Intent(activity, MainActivity.class);
        activity.startActivity(intent);
    }
}
